LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-15-2006, 01:38 AM   #1
michael_hk
LQ Newbie
 
Registered: Jun 2006
Distribution: Red Hat 9.0
Posts: 4

Rep: Reputation: 0
[Bash] New line character in variable


Hi,

How can I include a new line character in a variable in a bash shell script?

test.sh
=============
myvar="line1\nline2"
echo $myvar # gives line1\nline2


Doesn't work...and I have already tried many different combinations (\r\n, etc etc).

Thanks in advance.

Michael
 
Old 06-15-2006, 02:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Use echo -e instead of echo.
Code:
#!/bin/bash
myvar="line1\nline2"
echo -e $myvar
man bash for details.

Hope this helps.
 
Old 06-15-2006, 05:38 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
druuna is right.
See also:
Code:
man echo
-e makes echo interpret some backslash escape sequences like \n, \r, \t, etc.
 
Old 06-15-2006, 06:25 AM   #4
michael_hk
LQ Newbie
 
Registered: Jun 2006
Distribution: Red Hat 9.0
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by timmeke
druuna is right.
See also:
Code:
man echo
-e makes echo interpret some backslash escape sequences like \n, \r, \t, etc.
Thanks. It works.
 
Old 06-15-2006, 02:13 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
No need for the echo command, myvar=$'\n' will also do the trick.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
end of line character hyperriven Linux - General 1 04-07-2004 08:24 PM
how to avoid new line character gschrade Linux - Newbie 24 02-27-2004 11:14 AM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration